# STM32 Minimal CMake project for C/C++ projects
cmake_minimum_required(VERSION 3.16)
###############################################################################
include(cmake/gcc-arm-none-eabi.cmake)
include(cmake/flash.cmake)

set(PROJECT_NAME asr6601-demo)

# Set project name and some defaults
if ("${PROJECT_NAME}" STREQUAL "")
    set(PROJECT_NAME firmware)
endif()
project(${PROJECT_NAME})
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Set debug mode as default
if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
    set(CMAKE_BUILD_TYPE Debug)
endif()
message("Build type: ${CMAKE_BUILD_TYPE}")

# Set microcontroller information
SET(TARGET_PROCESSOR cortex-m4)
# SET(TARGET_PROCESSOR star-mc1)
# set(MCU_FAMILY ASR6601)
# set(MCU_MODEL ASR6601_HD)
set(CPU_PARAMETERS
    -mcpu=${TARGET_PROCESSOR}
    -mthumb
    -mfpu=fpv4-sp-d16
    -mfloat-abi=softfp)

set(STARTUP_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/Platform/system/startup_cm4.S)
set(MCU_LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/Device/ASR6601SE.ld)

###############################################################################
set(EXECUTABLE ${CMAKE_PROJECT_NAME})

enable_language(C ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
set(CMAKE_CXX_EXTENSIONS ON)
###############################################################################

set(PROJECT_INCLUDE_DIRECTORIES
    ${CMAKE_CURRENT_SOURCE_DIR}/Platform/CMSIS
    ${CMAKE_CURRENT_SOURCE_DIR}/Platform/system
    ${CMAKE_CURRENT_SOURCE_DIR}/Core/inc
    ${CMAKE_CURRENT_SOURCE_DIR}/Drivers/peripheral/inc
    ${CMAKE_CURRENT_SOURCE_DIR}/Drivers/crypto/inc
    ${CMAKE_CURRENT_SOURCE_DIR}/Lora/driver
    # ${CMAKE_CURRENT_SOURCE_DIR}/Lorawan/linkwan/inc
    # ${CMAKE_CURRENT_SOURCE_DIR}/Lorawan/linkwan/region
    ${CMAKE_CURRENT_SOURCE_DIR}/Lora/radio
    ${CMAKE_CURRENT_SOURCE_DIR}/Lora/radio/sx126x
    ${CMAKE_CURRENT_SOURCE_DIR}/Lora/system
    ${CMAKE_CURRENT_SOURCE_DIR}/Lora/system/crypto
    ${CMAKE_CURRENT_SOURCE_DIR}/Lorawan/conf
    ${CMAKE_CURRENT_SOURCE_DIR}/Lorawan/mac
    ${CMAKE_CURRENT_SOURCE_DIR}/Lorawan/mac/region
    ${CMAKE_CURRENT_SOURCE_DIR}/Common
    )

file(GLOB_RECURSE PROJECT_SOURCES CONFIGURE_DEPENDS
    ${CMAKE_CURRENT_SOURCE_DIR}/Platform/system/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/Drivers/peripheral/src/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/Drivers/crypto/src/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/Lora/driver/*.c
    # ${CMAKE_CURRENT_SOURCE_DIR}/Lorawan/linkwan/*.c
    # ${CMAKE_CURRENT_SOURCE_DIR}/Lorawan/linkwan/region/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/Lorawan/mac/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/Lorawan/mac/region/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/Lora/radio/sx126x/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/Lora/system/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/Core/src/*.c
    # ${CMAKE_CURRENT_SOURCE_DIR}/Common/*.c
    )

add_executable(${EXECUTABLE}
    ${PROJECT_SOURCES}
    ${STARTUP_SCRIPT})

target_compile_definitions(${EXECUTABLE} PRIVATE
    $<$<CONFIG:Debug>:DEBUG>
    CONFIG_DEBUG_UART=UART0)

target_include_directories(${EXECUTABLE} SYSTEM PRIVATE
    ${PROJECT_INCLUDE_DIRECTORIES})

###############################################################################
target_compile_options(${EXECUTABLE} PRIVATE
    ${CPU_PARAMETERS}
    -ffile-prefix-map=${CMAKE_SOURCE_DIR}=.
    -Wall
    -Wextra
    # -Wpedantic
    -Wshadow
    -Wdouble-promotion
    -Wformat=2 -Wformat-truncation
    -Wundef
    -Wfatal-errors
    -fno-common
    -Wno-unused-parameter
    $<$<COMPILE_LANGUAGE:CXX>:
        -Wconversion
        -Wno-volatile
        -Wold-style-cast
        -Wuseless-cast
        -Wsuggest-override>
    # -fsingle-precision-constant
    -fno-builtin
    -fno-builtin-printf
    -fno-builtin-sprintf
    -fno-builtin-snprintf)

target_link_options(${EXECUTABLE} PRIVATE
    -T${MCU_LINKER_SCRIPT}
    ${CPU_PARAMETERS}
    -Wl,-Map=${CMAKE_PROJECT_NAME}.map
    -Wl,--print-memory-usage
    -Wl,--wrap=printf
    -Wl,--wrap=sprintf
    -Wl,--wrap=snprintf)

add_library(only_lib STATIC IMPORTED)
set_property(TARGET only_lib PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/Drivers/crypto/lib/libcrypto.a)

target_link_libraries(${EXECUTABLE} c m only_lib)

###############################################################################



if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
    message(STATUS "Maximum optimization for speed")
    target_compile_options(${EXECUTABLE} PRIVATE -Ofast)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
    message(STATUS "Maximum optimization for speed, debug info included")
    target_compile_options(${EXECUTABLE} PRIVATE -Ofast -g)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
    message(STATUS "Maximum optimization for size")
    target_compile_options(${EXECUTABLE} PRIVATE -Os -flto)
else ()
    message(STATUS "Minimal optimization, debug info included")
    target_compile_options(${EXECUTABLE} PRIVATE -O0 -gdwarf-2)
endif ()

generateExtraOutput(${EXECUTABLE})

# for windows only
add_custom_command(TARGET ${EXECUTABLE}
   POST_BUILD
   COMMAND powershell -Command "Get-Date; Write-Output 'Build completed'")
